package com.ydlclass.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.nio.charset.StandardCharsets;

/**
 * @Author duWenJian
 * @Date 2024/2/5 16:02
 * @Version 1.0
 */
    public class MyChannelHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ByteBuf buf = (ByteBuf) msg;
            System.out.println("收到消息：" + buf.toString(StandardCharsets.UTF_8));
            ctx.writeAndFlush(Unpooled.copiedBuffer("Hello Client".getBytes(StandardCharsets.UTF_8)));
        }

        @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
